package com.prestamomo.vil.ppa.pmvilpage;

import android.content.pm.PackageManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilPermissMd;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

public class PmvilPermissVy extends PmvilAtyRoot {

    private ImageView perPmvilIvBack;
    private TextView perPmvilTvty;
    private TextView perPmvilTvjj;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.permiss_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        perPmvilIvBack = findViewById(R.id.permis_pmvil_iv_back);
        perPmvilTvty = findViewById(R.id.permis_pmvil_tv_ty);
        perPmvilTvjj = findViewById(R.id.permis_pmvil_tv_jj);
    }

    @Override
    protected void clickePmvil() {
        perPmvilIvBack.setOnClickListener(v -> finish());
        perPmvilTvjj.setOnClickListener(v -> finish());
        perPmvilTvty.setOnClickListener(v -> PmvilPermissMd.getPmvilPermiss(pmvilAy, PmvilPermissMd.pmvilPermiss));
    }
    boolean pmvilIsFirst = true;
    @Override
    protected void onResume() {
        super.onResume();
        if(!pmvilIsFirst){
            if(PmvilPermissMd.checkPmvilAllPermiss(pmvilAy)){
                if(!PmvilPermissMd.phonePmvpXMi(pmvilAy)){
                    perPmvilTvty.setVisibility(View.VISIBLE);
                    perPmvilTvjj.setVisibility(View.VISIBLE);
                }else{
                    perPmvilTvty.setVisibility(View.GONE);
                    perPmvilTvjj.setVisibility(View.GONE);
                    finish();
                }
            }else{
                perPmvilTvty.setVisibility(View.VISIBLE);
                perPmvilTvjj.setVisibility(View.VISIBLE);
            }
        }
        pmvilIsFirst = false;
    }
    boolean pmvilIsAllPermiss = false;
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==200){
            for (String permission : permissions) {
                pmvilIsAllPermiss = ActivityCompat.checkSelfPermission(this,permission) == PackageManager.PERMISSION_GRANTED;
                if(!pmvilIsAllPermiss){
                    if(!ActivityCompat.shouldShowRequestPermissionRationale(this,permission)){
                        pmvilIsAllPermiss = false;
                        showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
                        return;
                    }
                }
            }
            if(pmvilIsAllPermiss){
                if(!PmvilPermissMd.phonePmvpXMi(pmvilAy)){
                    showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
                }
            }else{
                showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
            }
        }
    }
    @Override
    protected void loadPmvilData() {
        showPmvilLoading();
        PmvilHttpRequest.getPmvilPjkwo(this, "kodwl", "pmiwl", new PmvilCallBack<String>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, String pmvilContent) {
                hindPmvilLoading();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, String pmvilCtent) {
                    outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, String pmvilCnt, String pmvilCode, String pmvilMsg) {
                hindPmvilLoading();
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                hindPmvilLoading();
            }
        });

    }
}
